package com.ruoyi.libb.controller;

import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.libb.entity.Product;
import com.ruoyi.libb.mapper.ProductMapper;
import com.ruoyi.libb.utils.Result;
import com.ruoyi.libb.utlis.ActivitiUtil;
import com.ruoyi.libb.utlis.Approver;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activiti")
@Api(description = "流程的部署测试")
public class ActivitiController {
    private static Logger logger= LoggerFactory.getLogger(ActivitiController.class);
    @Resource
    RepositoryService repositoryService;
    @Resource
    RuntimeService runtimeService;
    @Resource
    TaskService taskService;

    @Resource
    ProductMapper productMapper;


    @ApiOperation(value = "流程的部署创建")
    @GetMapping("/createDeployment")
    public Result createDeployment(@ApiParam(name = "bpmn", value = "部署地址", required = true) @RequestParam(value = "bpmn")String bpmn,
                                   @ApiParam(name = "name", value = "部署名称", required = true) @RequestParam(value = "name")String name){
        ActivitiUtil activitiUtil=new ActivitiUtil();
        HashMap<String, Object> deployment =null;
        try {
            deployment = activitiUtil.createDeployment(repositoryService,bpmn, name);
        }catch (Exception e){
            logger.error(e.getMessage());
            e.getMessage();
        }
        return Result.success(deployment);
    }


    @ApiOperation(value = "流程的部署列表")
    @GetMapping("/getDeployment")
    public Result getDeployment(){
        ActivitiUtil activitiUtil=new ActivitiUtil();
        List<HashMap<String, Object>> deployment = activitiUtil.getDeployment(repositoryService);
        return Result.success(deployment);
    }


    /**
     * 删除流程部署数据
     */
    @ApiOperation(value = "流程的部署数据删除")
    @GetMapping("/deleteDeployment")
    public Result deleteDeployment(@ApiParam(name = "deploymentId", value = "部署数据ID", required = true) @RequestParam(value = "deploymentId")String deploymentId) {
        repositoryService.deleteDeployment(deploymentId, true);
        return Result.success();
    }


    /**
     * 修改流程人员
     */
    @ApiOperation(value = "修改流程人员")
    @GetMapping("/updateApprover")
    public Result updateApprover(@ApiParam(name = "approver", value = "流程人员", required = true) @RequestParam(value = "approver")String approver) {
        Approver approverS=new Approver();
        approverS.setApprover(approver);
        return Result.success();
    }


    /**
     * 定义流程实例
     * @param key
     * @param businessKey
     * @return
     */
    @ApiOperation(value = "定义流程实例")
    @GetMapping("/ajaxCreateRuntime")
    public Result ajaxCreateRuntime(@ApiParam(name = "key", value = "部署数据key值", required = true) @RequestParam(value = "key")String key
            ,@ApiParam(name = "businessKey", value = "自定义businessKey值", required = true) @RequestParam(value = "businessKey") String businessKey) {
            ActivitiUtil activitiUtil=new ActivitiUtil();
        Result result = activitiUtil.ajaxCreateRuntime(runtimeService, key, businessKey);
        return result;
    }


    /**
     * 定义流程实例列表
     *
     * @param key
     * @return
     */
    @ApiOperation(value = "定义流程实例列表")
    @GetMapping("runtimeSelectList")
    public Result runtimeSelectList(@ApiParam(name = "key", value = "部署数据key值", required = true) @RequestParam(value = "key")String key) {

        ActivitiUtil activitiUtil = new ActivitiUtil();
        List<HashMap<String, Object>> runtime = activitiUtil.getRuntime(runtimeService, key);

        return Result.success(runtime);

    }


    /**
     * 流程实例的任务列表
     * @param key
     * @return
     */
    @ApiOperation(value = "查看任务节点列表")
    @GetMapping("taskSelectList")
    public Result taskSelectList(@ApiParam(name = "pid", value = "实例ID", required = true) @RequestParam(value = "pid")String pid
            , @ApiParam(name = "key", value = "部署数据key值", required = true) @RequestParam(value = "key")String key) {


        ActivitiUtil activitiUtil = new ActivitiUtil();

        List<HashMap<String, Object>> task = activitiUtil.getTask(runtimeService, taskService, pid, key);

        return Result.success(task);
    }


    /**
     * 根据名称获取代办任务
     */
    @ApiOperation(value = "根据名称获取代办任务")
    @GetMapping("getTaskName")
    public Result getTaskName(HttpServletRequest request) {
        ActivitiUtil activitiUtil=new ActivitiUtil();
        String token = request.getHeader("token");
        /*System.out.println(token);*/
        Map map = JwtUtils.parseToken(token);
        //获取到用户名称
        String login_name = String.valueOf(map.get("username"));
        List<HashMap<String, Object>> taskByName = activitiUtil.getTaskByName(runtimeService, taskService,login_name );

        return Result.success(taskByName);
    }


    /**
     * 执行节点任务
     */
    @ApiOperation(value = "执行节点任务")
    @GetMapping("taskUpdate")
    public Result taskUpdate(@ApiParam(name = "tasdId", value = "任务ID", required = true) @RequestParam(value = "tasdId")String tasdId
            ,@ApiParam(name = "passId", value = "任务的执行0失败1通过", required = true) @RequestParam(value = "passId")String passId
            ,@ApiParam(name = "businessKey", value = "产品ID", required = true) @RequestParam(value = "businessKey")String businessKey
            ,@ApiParam(name = "name", value = "任务名称", required = true) @RequestParam(value = "name")String name) {
        ActivitiUtil activitiUtil=new ActivitiUtil();
//        String token = request.getHeader("token2");
//        Map map = JwtUtils.parseToken(token);
        //获取到用户名称
//        String login_name = String.valueOf(map.get("login_name"));
        //执行任务
        Boolean aBoolean = activitiUtil.taskRun(taskService, tasdId, passId);
        if (name.equals("CEO的审批")&&passId.equals("1")){//判断CEO审批通过  1为通过
            productMapper.upPro(businessKey,2);
        }
        if (name.equals("CEO的审批")&&passId.equals("0")){//判断CEO审批通过  1为通过
            productMapper.upPro(businessKey,4);
        }
        if (name.equals("CFO的审批")&&passId.equals("1")){
            productMapper.upPro(businessKey,3);
        }
        if (name.equals("CFO的审批")&&passId.equals("0")){
            productMapper.upPro(businessKey,4);
        }

        if (!aBoolean){
            return Result.error(10009,"失败");
        }
        return Result.success();
    }


    /**
     * 任务的取消
     */
    @ApiOperation(value = "任务的取消")
    @GetMapping("unclaimTask")
    public Result unclaimTask(@ApiParam(name = "tasdId", value = "任务ID", required = true) @RequestParam(value = "tasdId")String tasdId) {
        ActivitiUtil activitiUtil=new ActivitiUtil();
//        String token = request.getHeader("token2");
//        Map map = JwtUtils.parseToken(token);
        //获取到用户名称
//        String login_name = String.valueOf(map.get("login_name"));
        //执行任务
        Boolean aBoolean = activitiUtil.unclaimTaks(taskService,tasdId);
        if (!aBoolean){
            return Result.error(10009,"失败");
        }
        return Result.success();
    }
    /**
     * 删除任务
     */
    @ApiOperation(value = "删除任务")
    @GetMapping("delTask")
    public Result delTask(@ApiParam(name = "processInstanceId", value = "任务ID", required = true) @RequestParam(value = "processInstanceId")String processInstanceId) {
        ActivitiUtil activitiUtil=new ActivitiUtil();
//        String token = request.getHeader("token2");
//        Map map = JwtUtils.parseToken(token);
        //获取到用户名称
//        String login_name = String.valueOf(map.get("login_name"));
        //执行任务
        Boolean aBoolean = activitiUtil.delTaks(runtimeService,processInstanceId,"删除");
        if (!aBoolean){
            return Result.error(10009,"失败");
        }
        return Result.success();
    }

    /**
     *根据产品ID查询产品数据
     */
    @ApiOperation(value = "根据产品ID查询产品数据")
    @GetMapping("getProductId")
    public Result getProductId(@ApiParam(name = "getProductId", value = "产品ID", required = true) @RequestParam(value = "getProductId")String getProductId) {
        Product productId = productMapper.getProductId(getProductId);
        return Result.success(productId);
    }




}
